package view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import com.algorithms.TwitterApi;

//VS4E -- DO NOT REMOVE THIS LINE!
public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel0;
	private JLabel jLabel1;
	private JTextField jTextField0;
	private JPasswordField jPasswordField0;
	private JButton jButton0;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	public MainFrame() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJLabel0(), new Constraints(new Leading(36, 10, 10), new Leading(
				35, 10, 10)));
		add(getJLabel1(), new Constraints(new Leading(71, 12, 12), new Leading(
				72, 10, 10)));
		add(getJTextField0(), new Constraints(new Leading(161, 137, 10, 10),
				new Leading(33, 12, 12)));
		add(getJPasswordField0(), new Constraints(
				new Leading(161, 136, 12, 12), new Leading(68, 12, 12)));
		add(getJButton0(), new Constraints(new Leading(71, 226, 12, 12),
				new Leading(136, 88, 10, 10)));
		setSize(405, 297);
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("Entrar!");
			jButton0.addMouseListener(new MouseAdapter() {

				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private JPasswordField getJPasswordField0() {
		if (jPasswordField0 == null) {
			jPasswordField0 = new JPasswordField();
			jPasswordField0.setEchoChar('•');
		}
		return jPasswordField0;
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
		}
		return jTextField0;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Contraseña: ");
		}
		return jLabel1;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Usuario de twitter: ");
		}
		return jLabel0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				MainFrame frame = new MainFrame();
				frame.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
				frame.setTitle("Log in");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0MouseMouseClicked(MouseEvent event) {
		String user = jTextField0.getText();
		String pass = new String(jPasswordField0.getPassword());

		boolean todoCool = true;

		// check user name is not null
		if (user == null || "".equals(user)) {
			ErrorDialog e = new ErrorDialog("nombre de usuario vacío");
			e.setVisible(true);
			todoCool = false;
		}
		// check pass is not null
		if (pass == null || "".equals(pass)) {
			ErrorDialog e = new ErrorDialog("password vacío");
			e.setVisible(true);
			todoCool = false;
		}

		// if there weren't any problems with the user and pass
		if (todoCool == true) {

			// initalize the twitter api.
			boolean validUser = TwitterApi.setUser(user, pass);

			// if the user is valid
			if (validUser == true) {
				// go ahead
				this.setVisible(false);
				JFrame resultsFrame = new ResultsFrame();
				resultsFrame.setVisible(true);
				resultsFrame.setTitle("Selección de Algoritmos");
			} else {
				// print error with invald user msg.
				ErrorDialog e = new ErrorDialog("usuario incorrecto");
				e.setTitle("Error!");
				e.setVisible(true);
			}
		}
	}
}